#include "Optimizer.h"
#include <iostream>
using namespace std;

Optimizer::Optimizer():descKey(""), newDocument(""),title(""),description(""),html(""),key(),imageLink("")
{
	this->imageWidth = 0;
	this->imageHeight = 0;
	
}

Optimizer::~Optimizer()
{
	
}

Optimizer::Optimizer(const Optimizer& source):descKey(source.descKey), newDocument(source.newDocument),
title(source.title), description(source.description), html(source.html), key(source.key),imageLink(source.imageLink)
{
	this->imageWidth = source.imageWidth;
	this->imageHeight = source.imageHeight;
}

Optimizer& Optimizer::operator = (const Optimizer& source)
{
	this->descKey = source.descKey;
	this->newDocument = source.newDocument;
	
	this->title = source.title;
	this->description = source.description;

	this->html = source.html;
	this->key = source.key;

	this->imageLink = source.imageLink;

	this->imageWidth = source.imageWidth;
	this->imageHeight = source.imageHeight;

	return (*this);
}

void Optimizer::SetSearchKey( string item_link )
{
	(this->newDocument).erase();
	
	string shellCommand("wget ");
	shellCommand += "\"";
	shellCommand += item_link;
	shellCommand += "\"";

	system( shellCommand.c_str() );
	
	int lastIndex = -1;
	
	lastIndex = (int)( (item_link).rfind("/") );// 마지막 나타난 위치
	string originalFile( (item_link).substr( lastIndex + 1, (item_link).length() - lastIndex ) );

	FILE* file;
	FILE* file_;
	file = fopen( originalFile.c_str(), "rt" );

	int IsValidNaver = 0;
	int IsValidTistory = 0;
	int IsValidEgloos = 0;
	int IsValidDaum = 0;

	char buffer[4096] = "";
	
	if( file != NULL )
	{
		while( !feof(file) )
		{
			strcpy( buffer, "");
			fgets( buffer, sizeof(buffer), file);
			this->newDocument += buffer;
			
		}
		fclose( file );
		
		string remove("rm ");
		remove += originalFile;
		system( remove.c_str() );
		
	}

	
	//출처 검사
	IsValidNaver = (int)( this->newDocument.find( this->key.Loadnaver().GetKeyOfOrigin() ) );
	IsValidTistory =(int)( this->newDocument.find( this->key.Loadtistory1().GetKeyOfOrigin() ) );
	IsValidEgloos = (int)( this->newDocument.find( this->key.Loadegloos1().GetKeyOfOrigin() ) );
	IsValidDaum = (int)( this->newDocument.find( this->key.Loaddaum1().GetKeyOfOrigin() ) );
	
	//본문 키값 결정
	if( IsValidNaver > 0 )
	{
		//naver valid blog search
		//1. item_link의 문서에서 frame 태그안의 주소를 읽어온다.
		
		string temp_document =  this->newDocument;

		string tempLink("");
		
		int from = -1;
		int to = -1;
		int index = -1;
		//찾은 문자열의 끝 마지막 위치를 얻는다.
		index = (int)( temp_document.find( "<frame name=\"mainFrame\" src=\"" ) );

		if( index > 0 )
		{
			
			while( strncmp( temp_document.c_str() + index, "src=", 4) != 0 )
			{
				index++;
			}
			
			index++;
			
			while( strncmp( temp_document.c_str() + index, "\"", 1 ) != 0 )
			{
				index++;
			}
			
			from = index;

			index++;
			
			while( strncmp( temp_document.c_str() + index, "\"", 1 ) != 0 )
			{
				index++;
			}

			to = index;

			// "......." : from ~ to

			tempLink.assign( temp_document, from, (to - from + 1) );

			shellCommand = "wget ";
			shellCommand += tempLink;

			//cout<<"tempLINK : "<<tempLink<<endl;

			system( shellCommand.c_str() );

			lastIndex = -1;
			lastIndex = (int)( tempLink.rfind( "/" ) );
			originalFile = tempLink.substr( lastIndex + 1, tempLink.length() - lastIndex - 2);

			//echo print
			cout<<" original : "<<originalFile<<endl;	
			temp_document = "";
			this->newDocument = "";

			//한글깨짐 변환작업
			string encodingCommand( "iconv -c -s -f cp949 -t utf-8 \"" );
			encodingCommand += originalFile;
			encodingCommand += "\" > ";
			encodingCommand += "tempFile1";
			system( encodingCommand.c_str() );

			string rmFile1("rm \"");
			rmFile1 += originalFile;
			rmFile1 += "\"";
			system( rmFile1.c_str() );

			originalFile = "tempFile1";
			
			file_ = fopen( originalFile.c_str() ,"rt" );

			if( file_ != NULL )
			{
				while( !feof(file_) )
				{
					strcpy( buffer, "" );
					fgets( buffer, sizeof(buffer), file_);
					this->newDocument += buffer;
				}
				fclose( file_ );
				string remove("rm ");
				remove += originalFile;
				system( remove.c_str() );
				
			}//endif

			temp_document = this->newDocument;
			this->newDocument = "";

			from = -1;
			to = -1;
			index = -1;

			index =(int)(temp_document.find("<iframe ID=\"papermain\" name=\"papermain\" ALLOWTRANSPARENCY=\"true\" src=\"/"));

			if( index > 0 )
			{
				cout<<"여기 들어오나? " <<endl;
				index++;
				
				while( strncmp( temp_document.c_str() + index, "src=\"", 5 ) != 0 )
				{	
					index++;
				}

				index++;
				
				while( strncmp( temp_document.c_str() + index, "/", 1) != 0 )
				{
					index++;
				}

				from = index;// /PostView ~

				index++;

				while( strncmp( temp_document.c_str() + index, "\"", 1 ) != 0 )
				{
					index++;
				}

				to = index;//  ......"
				// "......." : from ~ to

				tempLink = "";
				tempLink.assign( temp_document, from, (to - from + 1) );


				shellCommand = "wget \"http://blog.naver.com";
				shellCommand += tempLink;//   + /PostView...."
				system( shellCommand.c_str() );
				
				lastIndex = -1;
				lastIndex = (int)( tempLink.rfind( "/" ) );
				originalFile = tempLink.substr( lastIndex + 1, tempLink.length() - lastIndex - 2);

				cout<<"original : "<<originalFile<<endl;

				temp_document = "";

				//한글깨짐 변환작업
				encodingCommand = "iconv -c -s -f cp949 -t utf-8 \"";
				encodingCommand += originalFile;
				encodingCommand += "\" > ";
				encodingCommand += "tempFile2";

				system( encodingCommand.c_str() );
				
				string rmFile2("rm \"");
				rmFile2 += originalFile;
				rmFile2 += "\"";
				system( rmFile2.c_str() );

				originalFile = "tempFile2";
				
				file = fopen( originalFile.c_str(), "rt" );
				
				if( file != NULL )
				{
	
//					cout<<" 요기요기 !!!"<<endl;
					while( !feof(file) )
					{
						strcpy( buffer, "" );
						fgets( buffer, sizeof(buffer), file);
						temp_document += buffer;
					}
					fclose( file );
					string remove("rm ");
					remove += originalFile;
					system( remove.c_str() );
					
				
					this->newDocument = temp_document;
				}//endif
			
			}//endif
					
		
		}//endif
	
		this->descKey = this->key.Loadnaver().GetKeyOfContents();
	}
	else if( IsValidDaum > 0 )
	{
		//frame의 src주소를 찾는다
		int from = -1;
		int to = -1;
		int index = -1;
		
		
		index = (int)( this->newDocument.find( "<frame SRC" ) );
		
		if( index > 0 )
		{
			
			string temp_document = this->newDocument;
			
			//주소를 얻어와 다시 문서를 다운받는다.
			while( strncmp( temp_document.c_str() + index, "/", 1 ) != 0 )
			{
				index++;
			}

			from = index;// /~~~

			index++;

			while( strncmp( temp_document.c_str() + index, "\"", 1 ) != 0 )
			{
				index++;
			}
	
			to = index;// ~~~"

			string tempLink("");

			tempLink.assign( temp_document, from, to - from + 1 );// "~~~~~~"

			string downCommand("wget \"http://blog.daum.net");
			downCommand += tempLink;
			system( downCommand.c_str() );

			//다운받은 파일 한글변환해서 다시 읽어들인다.
		    lastIndex = -1;
			lastIndex = (int)( tempLink.rfind("/") );
			originalFile = tempLink.substr( lastIndex + 1, tempLink.length() - lastIndex - 2);

			//echo
			cout<<" daum original : "<<originalFile<<endl;

			string encodingCommand( "iconv -c -s -f cp949 -t utf-8 \"" );
			encodingCommand += originalFile;
			encodingCommand +="\" > ";
			encodingCommand +="tempFile";
			system( encodingCommand.c_str() );

			string rmFile( "rm \"");
			rmFile += originalFile;
			rmFile += "\"";
			system( rmFile.c_str() );

			originalFile = "tempFile";

			file = fopen( originalFile.c_str(), "rt" );

			this->newDocument = "";

			if( file != NULL )
			{
				while( ! feof( file ) )
				{
					strcpy(buffer, "");
					fgets(buffer, sizeof(buffer), file );
					this->newDocument += buffer;
				}
				fclose( file );
				
				string remove("rm ");
				remove += originalFile;
				system( remove.c_str() );
			}

		}
		
		this->descKey = this->key.Loaddaum1().GetKeyOfContents();	
	}
	else if( IsValidTistory > 0 )
	{
		this->descKey = this->key.Loadtistory1().GetKeyOfContents();
	}
	else if( IsValidEgloos > 0 )
	{
		this->descKey = this->key.Loadegloos1().GetKeyOfContents();
	}
	else
	{
		cout<<" unknown site "<<endl;
	}
	
}


void Optimizer::ReadDescription( string item_title )
{
	this->title = item_title;

	int index = 0;
	int start = 0;
	int end = 0;

	bool keepgoing = false;
	bool IsEnd = false;
	bool noFind = false;

	//제목으로 유효한 블로그 검사
	
	//if( this->descKey == this->key.Loadegloos1().GetKeyOfOrigin() )
	index = (int)( this->newDocument.find( this->title.c_str() ) );
	
	if( index < 0 )
	{
		this->title = "blog.rss.naver.com";
		index = (int)( this->newDocument.find( this->title.c_str() ) );
		if( index < 0 )
		{
			this->title = "blog.daum.net/xml/rss";
			index = (int)( this->newDocument.find( this->title.c_str() ) );
		}
	}
		

	if( index > 0  )
	{
		index = -1;
		
//		cout<< " descKey : "<< this->descKey <<endl;
			
		index = (int)( this->newDocument.find( this->descKey.c_str() ) );

		//본문을 못찾는 키값이라면 다른 키 값으로 교체
		while( index < 0  )
		{
			
			//tistory key change from invalidKey to validKey
			if( this->descKey == this->key.Loadtistory1().GetKeyOfContents() )
			{
				this->descKey = this->key.Loadtistory2().GetKeyOfContents();
			}
			else if( this->descKey == this->key.Loadtistory2().GetKeyOfContents() )
			{
				this->descKey = this->key.Loadtistory3().GetKeyOfContents();
			}
			else if( this->descKey == this->key.Loadtistory3().GetKeyOfContents() )
			{
				this->descKey = this->key.Loadtistory4().GetKeyOfContents();
			}
			else if( this->descKey == this->key.Loadtistory4().GetKeyOfContents() )
			{
				this->descKey = this->key.Loadtistory5().GetKeyOfContents();
			}
			else if( this->descKey == this->key.Loadtistory5().GetKeyOfContents() )
			{
				this->descKey = this->key.Loadtistory6().GetKeyOfContents();
			}
			else if( this->descKey == this->key.Loadtistory6().GetKeyOfContents() )
			{
				this->descKey = this->key.Loadtistory7().GetKeyOfContents();
			}
			else if( this->descKey == this->key.Loadtistory7().GetKeyOfContents() )
			{
				this->descKey = this->key.Loadtistory8().GetKeyOfContents();
			}
			else if( this->descKey == this->key.Loadtistory8().GetKeyOfContents() ) 
			{
				this->descKey = this->key.Loadtistory9().GetKeyOfContents();
			}
			else if( this->descKey == this->key.Loadtistory9().GetKeyOfContents() )
			{
				this->descKey = this->key.Loadtistory10().GetKeyOfContents();
			}
			else if( this->descKey == this->key.Loadtistory10().GetKeyOfContents() )
			{
				this->descKey = this->key.Loadtistory11().GetKeyOfContents();
			}
			else if( this->descKey == this->key.Loadtistory11().GetKeyOfContents() )
			{
				this->descKey = this->key.Loadtistory12().GetKeyOfContents();
			}
			/*
			else
			{
				noFind = true;
			}
			*/
			//egloos key change from invalidKey to validKey
			if( this->descKey == this->key.Loadegloos1().GetKeyOfContents() )
			{
				this->descKey = this->key.Loadegloos2().GetKeyOfContents();
			}
			else if( this->descKey == this->key.Loadegloos2().GetKeyOfContents() )
			{
				this->descKey = this->key.Loadegloos3().GetKeyOfContents();
			}
			else if( this->descKey == this->key.Loadegloos3().GetKeyOfContents() )
			{
				this->descKey = this->key.Loadegloos4().GetKeyOfContents();
			}
			/*
			else
			{
				noFind = true;
			}
			*/

			//daum key change from invalidKey to validKey
			if( this->descKey == this->key.Loaddaum1().GetKeyOfContents() )
			{
				this->descKey = this->key.Loaddaum2().GetKeyOfContents();
			}
			/*
			else
			{
				noFind = true;
			}
			*/
			
			index = (int)( this->newDocument.find( this->descKey.c_str() ) );
		}
		
		cout<<"키값은 : ??"<<this->descKey<<endl;
		start = index;

		while( strncmp( this->newDocument.c_str() + index, ">", 1) != 0 )
		{
			index++;
		}

		while( IsEnd != true )
		{
			if( strncmp( this->newDocument.c_str() + index, "</div", 5) == 0 ||
				strncmp( this->newDocument.c_str() + index, "</DIV", 5) == 0 )
			{
				if( keepgoing == true )
				{
					keepgoing = false;
				}
				else if( keepgoing == false )
				{
					while( strncmp( this->newDocument.c_str() + index , ">", 1 ) != 0 )
					{
						index++;
					}
					end = index;

					cout << ">> Start:" << start  <<"   end:"<< end <<  "   description:"<< this->newDocument.length() <<" end-start+1:" << (end-start+1) << endl;

					this->description.assign( this->newDocument, start, end - start + 1 );

					IsEnd = true;

					int getImage = -1;
					int index = end;
					string remain("");
					remain.assign( this->newDocument, end, this->newDocument.length() - end + 1);
					getImage = (int)( remain.find( "<div class=\"image" ) );
					while( getImage > 0 )
					{
						index = getImage;
						while( strncmp( remain.c_str() + index, "</div>", 6 ) != 0 )
						{
							index++;
						}
						index += 6;
						string plusString("");
						plusString.assign( remain, getImage, index - getImage );

						this->description += plusString;
						end = index;
						remain.assign( remain, end, remain.length() - end + 1 );
						getImage = -1;
						getImage = (int)( remain.find( "<div class=\"image" ) );
					}
				}
			}
			else if( strncmp( this->newDocument.c_str() + index, "<div", 4 ) == 0 ||
					 strncmp( this->newDocument.c_str() + index, "<DIV", 4 ) == 0 )
			{
				keepgoing = true;
			}

			if( IsEnd != true )
			{
				index++;
			}
		}
	}
	else
	{
		cout<<"not found!"<<endl;
	}

	cout<<" desckey : "<< this->descKey<<endl;

}

void Optimizer::RemoveUnessentialTag()
{
	int start = -1;
	int end = -1;
	int index = -1;
	
	start = (int)( this->description.find("<img src=\"http://fs.tistory.com/blog/image/plugins/myEolin/images/icoVote.gif\""));
	if( start > 0 )
	{
		index = start;
		string object("<img src=\"http://fs.tistory.com/blog/image/plugins/myEolin/images/icoVote.gif\"");
		index++;
		while( strncmp( this->description.c_str() + index , "/>", 2) != 0 )
		{
			index++;
		}
		index++;
		end = index;
		this->description.erase( start , end - start + 1 );
	}
	start = -1;
	start = (int)( this->description.find("<img src=\"http://fs.tistory.com/blog/image/plugins/myEolin/images/icoBookmark.gif\""));
	if( start > 0 )
	{
		index = start;
		string object("<img src=\"http://fs.tistory.com/blog/image/plugins/myEolin/images/icoBookmark.gif\"");
		index++;
		while( strncmp( this->description.c_str() + index, "/>", 2) != 0 )
		{
			index++;
		}
		index++;
		end = index++;
		this->description.erase( start, end - start + 1 );
	}
	
	start = -1;
	end = -1;
	index = -1;

	string fontF("font-family");
	start = (int)( this->description.find( fontF.c_str() ) );
	while( start > 0 )
	{
		this->description.erase( start, 11 );
		start = -1;
		start = (int)( this->description.find( fontF.c_str() ) );
	}
	
	start = -1;
	end = -1;
	index = -1;
	
	string objectS1("이올린에 북마크하기");
	string objectS2("이올린에 추천하기");
	
	start = (int)( this->description.find( objectS1.c_str()));
	//end = (int)( this->description.find( objectS2.c_str()));
	
	cout<<"이올린 지울차례"<<endl;	
	
	if( start > 0 )
	{
		cout<<"성공"<<endl;
		index = start;
		index++;
		while( strncmp( this->description.c_str() + index, "<", 1) != 0 )
		{
			index++;
		}
		this->description.erase( start, index - start );
	}
	
	start = -1;
	index = -1;
	start = (int)( this->description.find( objectS2.c_str() ) );
	if( start > 0 )
	{
		cout<<"성공"<<endl;
		index = start;
		index++;
		while( strncmp( this->description.c_str() + index, "<", 1) != 0 )
		{
			index++;
		}
		this->description.erase( start, index - start );
	}
		
	start = -1;
	end = -1;
	index = -1;
	// <a ~ > searching
	start = (int)( this->description.find( "<a" ) );
	
	while( start > 0 )
	{	
		index = start;
		
		while( strncmp( this->description.c_str() + index, ">" , 1) != 0 )
		{
			index++;
		}
		
		end = index;
		
		this->description.erase( start, end - start + 1 );
	//	this->description.erase( start, 2);
		start = -1;

		start = (int)( this->description.find( "<a" ) );		
	}

	start =1;
	end = -1;
	index = -1;
	
	start = (int)( this->description.find( "<FONT"));
	while( start > 0 )
	{
		index = start;
		while( strncmp( this->description.c_str() + index, ">", 1 ) != 0 )
		{
			index++;
		}
		end = index;
		this->description.erase( start, end - start + 1 );
		start = -1;
		start = (int)( this->description.find( "<FONT"));
	}

	start = -1;
	end = -1;
	index = -1;
	start = (int)( this->description.find( "<font"));
	while( start > 0 )
	{
		index = start;
		while( strncmp( this->description.c_str() + index, ">", 1) != 0 )
		{
			index++;
		}
		end = index;
		this->description.erase( start, end -  start + 1 );
		start = -1;
		start = (int)( this->description.find( "<font"));
	}
	
	start = -1;
	end = -1;
	index = -1;

	start = (int)(this->description.find( "</font>"));
	while( start > 0 )
	{
		this->description.erase( start, 7 );
		start = -1;
		start = (int)( this->description.find( "</font>" ));
	}

	start = -1;
	end = -1;
	index = -1;
	start = (int)(this->description.find("</FONT>"));
	while( start > 0 )
	{
		this->description.erase( start, 7 );
		start = -1;
		start = (int)( this->description.find( "</FONT>"));
	}
	//초기화
	start = -1;
	end = -1;
	index = -1;
	
	//<A ~ >
	start = (int)( this->description.find( "<A" ) );

	while( start > 0 )
	{
		index = start;

		while( strncmp( this->description.c_str() + index, ">", 1 ) != 0 )
		{
			index++;
		}

		end = index;

		this->description.erase( start, end - start + 1 );
//		this->description.erase( start, 2 );

		start = -1;

		start = (int)( this->description.find( "<A" ) );
	}

	start = -1;
	end  = -1;
	index = -1;

	//</a>, </A>

	start = (int)( this->description.find( "</a>" ) );

	while( start > 0 )
	{
		this->description.erase( start, 4 );

		start = -1;

		start = (int)( this->description.find( "</a>" ) );
	}

	start = (int)( this->description.find( "</A>" ) );

	while( start > 0 )
	{
		this->description.erase( start, 4 );

		start = -1;

		start = (int)( this->description.find( "</A>" ) );
	}
	
	start = -1;
	end = -1;
	index = -1;

	//<P>
	start = (int)( this->description.find( "<P>" ) );
	while( start > 0 )
	{
		this->description.erase( start, 3 );
		start = -1;
		start = (int)( this->description.find( "<P>" ) );
	}
	
	//</P>
	start = (int)( this->description.find( "</P>" ) );
	while( start > 0 )
	{
		this->description.erase( start, 4 );
		start = -1;
		start = (int)( this->description.find( "</P>" ) );
	}

	start - -1;
	end = -1;
	index = -1;

	start = (int)( this->description.find( "style="));
	while( start > 0 )
	{
		this->description.erase( start, 6 );
		start = -1;
		start = (int)( this->description.find( "style="));
	}

	/*
	start = -1;
	end = -1;
	index = -1;

	start = (int)( this->description.find( "<style"));
	while( start > 0 )
	{
		index = start;
		index++;
		while( strncmp( this->description.c_str() + index, ">", 1 ) != 0 )
		{
			index++;
		}
		this->description.erase( start, index - start + 1 );
		start = -1;
		start =  (int)( this->description.find( "<style" ) );
	}
*/
	start = -1;
	end = -1;
	index = -1;
	
	start = (int)( this->description.find( "</style>" ) );
	while( start > 0 )
	{
		this->description.erase( start, 8 );
		start = -1;
		start = (int)( this->description.find( "</style>"));
	}
	
	start = -1;
	end = -1;
	index = -1;
	
	//style="CURSOR: hand" ,  onclick=popview(this.src) 제거
	start = (int)( this->description.find( "style=\"CURSOR: hand\"") );
	
	while( start > 0 && end > 0 )
	{
		index = start;
		while( strncmp( this->description.c_str() + index, "hand", 4 ) != 0 )
		{
			index++;
		}
		index++;
		while( strncmp( this->description.c_str() + index, "\"", 1 ) != 0 )
		{
			index++;
		}
		end = index;
		this->description.erase( start, (end - start + 1) );
		start = -1;
		end = -1;

		start = (int)( this->description.find( "style=\"CURSOR: hand\""));
	}

	start = -1;
	end = -1;
	index = -1;
	start = (int)(this->description.find( "onclick=popview(this.src)") );
	while( start > 0 )
	{
		index = start;
		while( strncmp( this->description.c_str() + index, ")", 1) != 0 )
		{
			index++;
		}
		end = index;
		this->description.erase( start, (end - start + 1 ) );
		start = -1;
		end = -1;
		start = (int)( this->description.find("onclick=popview(this.src)"));
	}

	start = -1;
	end = -1;
	index = -1;
	//onload='setTimeout("resizeImage(2025599)",200)'제거
	index = (int)( this->description.rfind( "onload" ) );
	
	while( index > 0 )
	{
		start = index;

		index++;
		
		while( strncmp( this->description.c_str() + index, ")'", 2 ) != 0 )
		{
			index++;
		}
		index++;

		end = index;
	
		this->description.erase( start, (end - start + 1));

		index = -1;
		index = (int)( this->description.rfind( "onload" ) );
		
	}


	start = -1;
	end = -1;
	index = -1;
	int index_ = -1;
	
	// onclick 제거
	start = (int)( this->description.find( "onclick" ));
	
	while( start > 0 )
	{
		index = start;

		while( strncmp( this->description.c_str() + index, "\"", 1 ) != 0  )
		{
			index++;
		}

		index++;
		
		while( strncmp( this->description.c_str() + index, "\"", 1 ) != 0 )
		{
			index++;
		}
		
		end = index;
		
		this->description.erase( start, end - start + 1 );

		start = -1;

		start = (int)( this->description.find( "onclick" ) );
	}

	//onclick=popview(this.src)   onclick=GoImg(this.src)
	while( index_ > 0 )
	{
		start = index_;

		index_++;

		while( strncmp( this->description.c_str() + index_, ")", 1 ) != 0 )
		{
			index_++;
		}

		end = index_;
		
		this->description.erase( start, (end - start + 1) );

		index_ = -1;

		index_ = (int)( this->description.rfind( "onclick" ) );

	}
	

	start = -1;
	end = -1;
	index = -1;

	// onmouseover 제거
	start = (int)( this->description.find( "onmouseover" ) );

	while( start > 0  )
	{
		index = start;

		int	isValid = index;
		isValid++;

		if( strncmp( this->description.c_str() + isValid, "\"", 1 ) == 0 )
		{		
			while( strncmp( this->description.c_str() + index, "\"", 1 ) != 0 )
			{
				index++;
			}

			index++;

			while( strncmp( this->description.c_str() + index, "\"", 1 ) != 0 )
			{
				index++;
			}

			end = index;

			this->description.erase( start, end - start + 1 );
		}
		else if( strncmp( this->description.c_str() + isValid, NULL, 1) != 0 )
		{
			index++;
			
			while( strncmp( this->description.c_str() + index, " ", 1 ) != 0 )
			{
				index++;
			}

			end = index - 1;

			this->description.erase( start, end - start + 1 );
		}
		start = -1;

		start = (int)( this->description.find( "onmouseover" ) );
	}	


}


// 본문에서 이미지를 찾아내서 다운받아 이미지를 리사이징한다.
void Optimizer::RenderingImage()
{

	//전송폴더를 만든다.
	string makeDir("mkdir sendContents");
	system( makeDir.c_str() );
	makeDir = "mkdir sendContents/html";
	system( makeDir.c_str() );
	makeDir = "mkdir sendContents/images";
	system( makeDir.c_str() );

	int start_IMGtag =  -1;
	
	int start = -1;
	int end = -1;
	int index = 0;

	int size_height = 0;
	int size_width = 0;
	int temp = 0;

	string size="";
	
	int i = 0;

	int sysAction = -1;
	bool IsDouble = false;
	bool IsSingle = false;
	bool IsNon = false;

	//본문의 복사본
	string temp_description( this->description.c_str() );
//	cout<<" daewook : "<<temp_description<<endl;
	
	//cout<< " 임시본문전체 출력 : "<<temp_description<<endl;

	index = (int)( temp_description.find( "<img" ) );

	if( index > 0  )
	{
		start_IMGtag = index;
		
		index++;
		
		while( strncmp(temp_description.c_str() + index, "src", 3 ) != 0 )
		{
			index++;
		}
		index++;
		//index = index + 4;
		while( strncmp( temp_description.c_str() + index, "=", 1) != 0 )
		{
			index++;
		}
		index++;
		
		if( strncmp( temp_description.c_str() + index, "\"", 1 ) == 0 )
	//			strncmp( temp_description.c_str() + index, "'", 1) == 0 )
		{
			IsDouble = true;
		}
		else if( strncmp( temp_description.c_str() + index, "'", 1) == 0 )
		{
			IsSingle = true;
		}
		else
		{
			IsNon = true;
		}
	}
	else
	{
		index = (int)( temp_description.find("<IMG"));

		if( index > 0 )
		{
			start_IMGtag = index;
			index++;
			
			while( strncmp( temp_description.c_str() + index, "src", 3 ) != 0 )
			{
				index++;
			}
			index++;
			while( strncmp( temp_description.c_str() + index, "=", 1 ) != 0 )
			{
				index++;
			}
			index++;
//			index = index + 4;

			if( strncmp( temp_description.c_str() + index, "\"", 1 ) == 0 )
//					strncmp( temp_description.c_str() + index, "'", 1) == 0 )
			{
				IsDouble = true;
			}
			else if( strncmp( temp_description.c_str() + index, "'", 1 ) == 0 )
			{
				IsSingle = true;
			}
			else
			{
				IsNon = true;
			}
		}
	}

//	cout<<" this->description : "<<temp_description<<endl;


	while( index > 0 )//이미지 태그안의 이미지 경로를 찾았다면 계속 수행
	{

		i++;//이미지의 번호 제어변수
		bool IsFixHeight = false;
		bool IsFixWidth = false;
		
		cout<<"1 --  IMGtag start : "<<start_IMGtag<<endl;	
			
		while( strncmp( temp_description.c_str() + index, "\"", 1 ) != 0 )
		{
			index++;
		}

		start = index;//이미지 주소의 시작

		index++;
		while( strncmp( temp_description.c_str() + index, "\"", 1 ) != 0 )
		{
			index++;
		}

		end = index;//이미지주소의 끝

		int addString = start_IMGtag;// <IMG 태그 시작점
		int srcTagEnd = end;
		
		int IsDeclareWidth = -1;//가로 선언위치
		int IsDeclareHeight = -1;//세로 선언위치
		
		while( strncmp( temp_description.c_str() + srcTagEnd, ">", 1) != 0 )
		{
			srcTagEnd++;
		}//이미지 선언 태그의 끝 : ">"
		
		
		//이미지 태그 안에 크기 선언이 되어있는지 검사	
		while( strncmp( temp_description.c_str() + addString, "width", 5 ) != 0 && addString < srcTagEnd )
		{
			addString++;
		}

		if( addString >= srcTagEnd )
		{
			IsFixWidth = true;//이미지 태그안에 width선언이 없다.
			cout<<" 가로 선언이 없다!"<<endl;
		}
		else//선언이 되어 있다면 그 위치를 찾는다.
		{
			IsFixWidth = false;
		//	IsDeclareWidth = (int)( temp_description.find( "width" ) );
			int num = start_IMGtag;
			while( strncmp( temp_description.c_str() + num, "width", 5) != 0 )
			{
				num++;
			}
			IsDeclareWidth = num;//이미지 선언태그안의 가로크기 선언위치
		}

		addString = start_IMGtag;
		
		while( strncmp( temp_description.c_str() + addString, "height", 6) != 0 && addString < srcTagEnd )
		{
			addString++;
		}

		if( addString  >= srcTagEnd )
		{
			IsFixHeight = true;//이미지 태그안에 height선언이 없다.
			cout<<"세로 선언이 없다!!!"<<endl;
		}
		else//선언이 되어 있다면 그 위치를 찾는다.
		{
			IsFixHeight = false;
			//IsDeclareHeight = (int)( temp_description.find( "height" ) );
			int num = start_IMGtag;
			while( strncmp( temp_description.c_str() + num , "height", 6 ) != 0 )
			{
				num++;
			}
			IsDeclareHeight = num;
		}

		size = "";
		if( IsFixHeight == false && IsFixWidth == false )//이미지 선언이 둘다 되어있는 경우
		{		
		
	//		i++;
			cout<<"여기닷!!!1"<<endl;	
			cout<<" IsDeclareWidth : "<<IsDeclareWidth<<endl;
			cout<<" IsDeclareHeight : "<<IsDeclareHeight<<endl;
			
			if( IsDouble == true )// 크기 선언이 " "로 선언되어 있는경우
			{
			
				//세로 이미지의 크기를 읽는다.
				temp = IsDeclareHeight;//(int)( temp_description.find( "height" ) );

				temp++;
				while( strncmp( temp_description.c_str() + temp, "\"", 1 ) != 0 )
				{
					temp++;
				}

				temp++;
		
				while( strncmp( temp_description.c_str() + temp, "\"", 1 ) != 0 )
				{
					size += temp_description[temp];
					temp++;
				}
		
				cout<<" size Width : "<<size<<endl;
				size_height = atoi( size.c_str() );


				size = "";
			
				//가로 이미지의 크기를 읽는다.
				temp = IsDeclareWidth;//(int)( temp_description.find( "width" ) );

				temp++;
				while( strncmp( temp_description.c_str() + temp, "\"", 1 ) != 0 )
				{
					temp++;
				}

				temp++;

				while( strncmp( temp_description.c_str() + temp, "\"", 1 ) != 0 )
				{
					size += temp_description[temp];
					temp++;
				}

				size_width = atoi( size.c_str() );
		
				size = "";
			}
			else if( IsNon == true )
			{
				temp = IsDeclareHeight;//(int)( temp_description.find("height" ));
				
				while( strncmp( temp_description.c_str() + temp, "=", 1) != 0 )
				{
					temp++;
				}
				
				temp++;
				
				while( strncmp( temp_description.c_str() + temp, " ", 1 ) != 0 )
				{
					size += temp_description[temp];
					temp++;
				}
				size_height = atoi( size.c_str() );
				
				size = "";

				temp = IsDeclareWidth;//(int)( temp_description.find("width"));
				while( strncmp( temp_description.c_str() + temp, "=", 1 ) != 0 )
				{
					temp++;
				}
				temp++;
				while( strncmp( temp_description.c_str() + temp, " ", 1 ) != 0 )
				{
					size += temp_description[temp];
					temp++;
				}
				size_width = atoi( size.c_str() );
				
				size = "";
			}
			else if( IsSingle == true )
			{
				temp = IsDeclareHeight;
				temp++;
				while( strncmp( temp_description.c_str() + temp, "'", 1) != 0 )
				{
					temp++;
				}
				temp++;
				while( strncmp( temp_description.c_str() + temp, "'", 1) != 0 )
				{
					size += temp_description[temp];
					temp++;
				}
				size_height = atoi( size.c_str() );
				size = "";
				temp = IsDeclareWidth;
				while( strncmp( temp_description.c_str() + temp, "'", 1 ) != 0 )
				{
					temp++;
				}
				temp++;
				while( strncmp( temp_description.c_str() + temp, "'", 1) != 0 )
				{
					size += temp_description[temp];
					temp++;
				}
				size_width = atoi( size.c_str() );
				
				size = "";
			}
				
		}//endif
		else if( IsFixHeight == true && IsFixWidth == false )//가로만 선언
		{
		//	i++;
			cout<<"여기닷!!!2"<<endl;
			if( IsDouble == true )
			{
				temp = IsDeclareWidth;
				temp++;
				while( strncmp( temp_description.c_str() + temp, "\"", 1) != 0 )
//						strncmp( temp_description.c_str() + temp, "'", 1) != 0 )
				{
					temp++;
				}
				temp++;
				while( strncmp( temp_description.c_str() + temp, "\"", 1) != 0 )
//						strncmp( temp_description.c_str() + temp, "'", 1) != 0 )
				{
					size += temp_description[temp];
					temp++;
				}
				size_width = atoi( size.c_str() );
				size_height = 190;
				size = "";
			}
			else if( IsNon == true )
			{
				temp = IsDeclareWidth;
				temp++;
				while( strncmp( temp_description.c_str() + temp, "=", 1) != 0 )
				{
					temp++;
				}
				temp++;
				while( strncmp( temp_description.c_str() + temp, " ", 1) != 0 )
				{
					size += temp_description[temp];
					temp++;
				}
				size_width = atoi( size.c_str() );
				size_height = 190;
				size = "";
			}
			else if( IsSingle == true )
			{
				temp = IsDeclareWidth;
				temp++;
				while( strncmp( temp_description.c_str() + temp, "'", 1) != 0 )
				{
					temp++;
				}
				temp++;
				while( strncmp( temp_description.c_str() + temp, "'", 1 ) != 0 )
				{
					size += temp_description[temp];
					temp++;
				}
				size_width = atoi( size.c_str() );
				size_height = 190;
				size = "";
			}
		}
		else if( IsFixHeight == false && IsFixWidth == true )//세로만 선언
		{
		//	i++;
			cout<<"여기닷!!!3"<<endl;
			if( IsDouble == true )
			{
				temp = IsDeclareHeight;
				temp++;
				while( strncmp( temp_description.c_str() + temp, "\"", 1) != 0 )
//						strncmp( temp_description.c_str() + temp, "'", 1) != 0 )
				{
					temp++;
				}
				temp++;
				while( strncmp( temp_description.c_str() + temp, "\"", 1) != 0 )
//						strncmp( temp_description.c_str() + temp, "'", 1) != 0 )
				{
					size += temp_description[temp];
					temp++;
				}
				size_height = atoi( size.c_str() );
				size_width = 220;
				size = "";
				
			}
			else if( IsNon == true )
			{
				temp = IsDeclareHeight;
				temp++;
				while( strncmp( temp_description.c_str() + temp, "=", 1) != 0 )
				{
					temp++;
				}
				temp++;
				while( strncmp( temp_description.c_str() + temp, " ", 1 ) != 0 )
				{
					size += temp_description[temp];
					temp++;
				}
				size_height = atoi( size.c_str() );
				size_width = 220;
				size = "";
			}
			else if( IsSingle == true )
			{
				temp = IsDeclareHeight;
				temp++;
				while( strncmp( temp_description.c_str() + temp, "'", 1) != 0 )
				{
					temp++;
				}
				temp++;
				while( strncmp( temp_description.c_str() + temp, "'", 1) != 0 )
				{
					size += temp_description[temp];
					temp++;
				}
				size_height = atoi( size.c_str() );
				size_width = 220;
				size = "";
			}
		}
		else// if( IsFixHeight == true || IsFixWidth == true )//둘중 하나라도 이미지 크기가 선언이 안되어 있을 경우 고정 크기 사용
		{
		//	i++;
			cout<<"여기닷!!!4"<<endl;
			size_width = 220;
			size_height = 190;

		}
		
		//이미지의 크기 정보를 바꾼다.
		
		//this->imageWidth = size_width;
	
		cout<<" sizewidth : "<<size_width <<endl;
		cout<<" sizeheight : "<<size_height <<endl;
		if( IsFixWidth == true && IsFixHeight == false)//세로선언만 되어 있으면
		{
			if( size_height > 220 )
			{
				this->imageWidth = 220;
				this->imageHeight = 180;
			}
			else
			{
				this->imageWidth = 220;
				this->imageHeight = size_height;
			}
		}
		else if( IsFixHeight == true && IsFixWidth == false )//가로선언만 되어 있으면
		{
			this->imageHeight = 180;
			this->imageWidth = size_width;
		}
		else
		{
			this->imageWidth = 220;
			this->imageHeight = 180;//(220*size_height)/size_width;
//						strncmp( temp_description.c_str() + temp, "'", 1 ) != 0 )
		}
//		this->imageHeight = (this->imageWidth*size_height)/size_width;

		//가로크기 문자열 만들기
		string sizeInfoWidth( "\"");
		char width[3];
		sprintf( width, "%d", this->imageWidth );

		sizeInfoWidth += width;
		sizeInfoWidth += "\"";

		//세로크기 문자열 만들기
		string sizeInfoHeight( "\"");
		char height[3];
		sprintf( height, "%d", this->imageHeight );
		
		sizeInfoHeight += height;
		sizeInfoHeight += "\"";

		
		//주소를 복사해 온다.
		if( temp_description.length() > 0  )
		{ 
			this->imageLink.assign( temp_description, start, end - start + 1 );	

			string shellCommand( "wget -O imagefile " );
			shellCommand += this->imageLink;

			cout<<" link : "<<this->imageLink<<endl;

			sysAction = system( shellCommand.c_str() );

			if( sysAction != 0 )
			{
				cout<<" error link access "<<endl;
			}

			//int lastIndex = -1;
			//lastIndex = (int)( this->imageLink.rfind("/") );
			//string downloadedImage( this->imageLink.substr( lastIndex + 1, this->imageLink.length() - lastIndex - 2 ) );
//			string downloadedImage("imagefile");


			//다운 받은 이미지 리사이징
			char addWidth[3];
			char addHeight[3];

			sprintf( addWidth, "%d", this->imageWidth );
			sprintf( addHeight, "%d", this->imageHeight );


			//복사 image파일 생성
			string copyFile("image");
			char addNumber[2];

			sprintf( addNumber, "%d", i );
			copyFile += addNumber;
			copyFile += ".png";
				
			//if( this->imageWidth ==220 )
			if( this->imageWidth == 220 || IsFixWidth == false || IsFixHeight == false )
			{
				string resizeCommand( "convert -sample ");
				resizeCommand += addWidth;
				resizeCommand += "x";
				resizeCommand += addHeight;
//				resizeCommand += " \"";
				resizeCommand += " imagefile ";
//				resizeCommand += "\" ";
				resizeCommand += copyFile;

				sysAction = system( resizeCommand.c_str() );

				if( sysAction != 0 )
				{
					cout<<" error convert image! "<<endl;
				}
				else//이미지 변환이 정상적으로 이루어졌으면 본 이미지 파일을 지운다
				{
					string remove("rm imagefile");
//					remove += downloadedImage;	
//					remove += "\"";
					system( remove.c_str() );
				}
			}
			else//이미지를 그냥 복사하고 원본을 지운다.
			{
				string copyImageCommand("cp ");
				copyImageCommand += "imagefile ";
//				copyImageCommand += "\" ";
				copyImageCommand += copyFile;
				system( copyImageCommand.c_str() );

				string remove("rm imagefile");
//				remove += downloadedImage;
//				remove += "\"";
				system( remove.c_str() );
			}	

			//전송할 하위 폴더로 파일 이동
			string moveCommand( "mv " );
			moveCommand += copyFile.c_str();
			moveCommand += " ";
			moveCommand += "sendContents/images/";//보낼 내용이 담길 하위 폴더

			sysAction = system( moveCommand.c_str() );

			if( sysAction != 0 )
			{
				cout<<" error move image! "<<endl;
			}

			//이미지를 본문의 선언부에 다시 싣는다.
			int findPosition = -1;

			//	string changedPath("\"sendContents/");
			string changedPath("\"/mnt/mtd/nfsdisk/Url/images/");
			//string changedPath("\"");
			changedPath += copyFile;
			changedPath += "\"";

//			findPosition = start_IMGtag;
			
			findPosition = (int)( this->description.rfind( this->imageLink.c_str() ) );

			//이미지의 선언부가 찾아졌다면 경로와 크기정보를 바꿔 다시 본문에 쓴다.
			if( findPosition > start_IMGtag )
			{

				
				this->description.replace( findPosition, this->imageLink.size(), changedPath );	
				
				//this->description.replace( findPosition, this->imageLink.length(), changedPath );
				int index_ = findPosition;// + changedPath.length();
				
				/*
				while( strncmp(this->description.c_str() + index_, "<", 1) != 0  ) 
				{
					(index_)--;
				}
				*/
				int imageTagStart = start_IMGtag;
				int addPosition = findPosition + changedPath.length();
				
				while( strncmp( this->description.c_str() + addPosition, ">",1 ) != 0 )
				{
					addPosition++;
				}//이미지 태그선언의 끝
				
				int imageTagEnd = addPosition;
				addPosition = imageTagStart;//<IMG
				
				bool flag_width = false;
				bool flag_height = false;

				while( strncmp( this->description.c_str() + addPosition, "width",5) != 0 && addPosition < imageTagEnd )
				{
					addPosition++;
				}
					
				if( addPosition + 5 < imageTagEnd )
				{	
					flag_width = true;
				}
					
				//addPosition = imageTagStart;
				addPosition = index_;
					
				while( strncmp( this->description.c_str() + addPosition, "height", 6) != 0 && addPosition < imageTagEnd )
				{
					addPosition++;
				}
					
				if( addPosition + 6 < imageTagEnd )
				{
					flag_height = true;
				}

				if( flag_width == true || flag_height == true )
				{
	
					int index_height = -1;
					int index_width = -1;

					index_width = IsDeclareWidth;//(int)( temp_description.find( "width" ) );
					index_height = IsDeclareHeight;//(int)( temp_description.find( "height") );

					cout<<"크기 선언 찾은 위치!!!!!!!!!!!!!!!!!!!!!"<<endl;
					cout<<"가로 : "<<index_width <<endl;
					cout<<"세로 : "<<index_height <<endl;
					
					if( index_width > 0 )
					{
						int point = index_width;
						while( strncmp( this->description.c_str() + point, " ", 1) != 0 )
						{
							point++;
						}
						this->description.erase( index_width, point - index_width );
					}
					
					if( index_height > 0 )
					{	
						int point = index_height;
						while( strncmp( this->description.c_str() + point , " ", 1 ) != 0 )
						{
							point++;
						}
						this->description.erase( index_height, point - index_height );
					}

					int findWidth = -1;
					findWidth = (int)( this->description.find( "width=" ));
					while( findWidth > 0 )
					{
						int po = findWidth;
						while( strncmp( this->description.c_str() + po, " ", 1) != 0 )
						{
							this->description.erase( findWidth, po - findWidth + 1 );
						}
						findWidth = -1;
						findWidth = (int)( this->description.find( "width=") );
					}
					int findHeight = -1;
					findHeight = (int)( this->description.find( "height="));
					while( findHeight > 0 )
					{
						int po = findHeight;
						while( strncmp( this->description.c_str() + po, " ", 1) != 0 )
						{
							this->description.erase( findHeight, po - findHeight + 1 );
						}
						findHeight = -1;
						findHeight = (int)( this->description.find("height="));
					}
					
					/*
					if( index_height < index_width  )
					{
					
						cout<<" height 선언이 먼저 되었다!$$$ "<<endl;
						int from = -1;
						int to = -1;
					
						if( IsDouble == true )
						{
							while( strncmp( this->description.c_str() + index_ , "height=", 7 ) != 0 )
							{
								index_++;
							}

							index_ = index_ + 6;

							from = index_;

							index_++;
						
							while( strncmp( this->description.c_str() + index_, "\"", 1 ) != 0 )
//									strncmp( this->description.c_str() + index_, "'", 1) != 0 )
							{
								index_++;
							}

							to = index_;
							
							if( from != to )
							{
								this->description.erase( from, (from - to + 1) );
								this->description.insert( from, sizeInfoHeight );
							}
							
				
							from = -1;
							to = -1;

							while( strncmp( this->description.c_str() + index_, "width=", 6 ) != 0 )
							{
								index_++;
							}
	
							index_ = index_ + 5;

							from = index_;
						
							index_++;

							while( strncmp( this->description.c_str() + index_, "\"", 1 ) != 0 )
//									strncmp( this->description.c_str() + index_, "'", 1 ) != 0 )
							{
								index_++;
							}
	
							to = index_;

							//this->description.replace( index_, from - to + 1, sizeInfoWidth );
							
							if( from != to )
							{
								this->description.erase( from, (to - from + 1) );
								this->description.insert( from, sizeInfoWidth );
							}
							
						
						}
						else if( IsSingle == true )
						{
							while( strncmp( this->description.c_str() + index_, "height=", 7 ) != 0 )
							{
								index_++;
							}
							index_ = index_ + 6;

							from = index_;

							index_++;
							while( strncmp( this->description.c_str() + index_, "'", 1 ) != 0 )
							{
								index_++;
							}
							to = index_;
							
							if( from != to )
							{
								 this->description.erase( from, to - from + 1 );
								this->description.insert( from, sizeInfoHeight );
							}
							from  = -1;
							to = -1;
							while( strncmp( this->description.c_str() + index_, "width=", 6 ) != 0 )
							{
								index_++;
							}
							index_ = index_ + 5;
							from = index_;
							index_++;
							while( strncmp( this->description.c_str() + index_, "'", 1) != 0 )
							{
								index_++;
							}
							to = index_;
							if( from != to )
							{
								this->description.erase( from, to - from + 1 );
								this->description.insert( from, sizeInfoWidth );
							}

						}
						else if( IsNon == true )
						{
							while( strncmp( this->description.c_str() + index_, "height=", 7 ) != 0 )
							{
								index_++;
							}
							index_++;
							
							while( strncmp( this->description.c_str() + index_, "=" , 1) != 0 )
							{
								index_++;
							}
							index_++;
							
							from = index_;
							
							index_++;
							
							while( strncmp( this->description.c_str() + index_, " ", 1 ) != 0 )
							{
								index_++;
							}
							index_ = index_ - 1;
							
							to = index_;
	

							if( to != from )
							{
								//cout<<"from : "<<from<<endl;
								//cout<<"to : "<<to<<endl;
								cout<<"지우기 전입니다!!!"<<endl;
								this->description.erase( from, (to - from + 1 ) );
								char sizeOfheight[3] = "";
								sprintf( sizeOfheight, "%d", this->imageHeight );
								this->description.insert( from, sizeOfheight );
							}
							
							//cout<<" 처리 !!!"<<endl;
						
							from = -1;
							to = -1;

							while( strncmp( this->description.c_str() + index_, "width=", 6 ) != 0 )
							{
								index_++;
							}
							index_++;
							while( strncmp( this->description.c_str() + index_, "=", 1 ) != 0 )
							{
								index_++;
							}
							index_++;
							
							from = index_;
							
							index_++;
							
							while( strncmp( this->description.c_str() + index_, " ", 1) != 0 ||
									strncmp( this->description.c_str() + index_, ">", 1 ) != 0 )
							{
								index_++;
							}
							index_ = index_ - 1;
							to = index_;
							
							if( to != from )
							{
								this->description.erase( from, (to - from + 1) );
								char sizeOfwidth[3] = "";
								sprintf( sizeOfwidth, "%d", this->imageWidth );
								this->description.insert( from, sizeOfwidth );
							}
							
						}
						
					}
					else if( index_width < index_height )
					{

						cout<<" width 선언이 먼저 되었다 aaaaaaaaaaaaaaaaa"<<endl;
						
						int from = -1;
						int to = -1;
						
						if( IsDouble == true )
						{
							while( strncmp( this->description.c_str() + index_, "width=", 6 ) != 0 )
							{
								index_++;
							}
							index_ = index_ + 5;
					
							from = index_;

							index_++;

							while( strncmp( this->description.c_str() + index_, "\"", 1 ) != 0 )
							{
								index_++;
							}
								
							to = index_;
						
							if( from != to )
							{
								this->description.erase( from, (to - from + 1) );
								this->description.insert( from, sizeInfoWidth );
							}
							
							from = -1;
							to = -1;

							while( strncmp( this->description.c_str() + index_, "height=", 7 ) != 0 )
							{
								index_++;
							}
						
							index_ = index_ + 6;

							from = index_;

							index_++;

							while( strncmp( this->description.c_str() + index_, "\"", 1 ) != 0 )
							{
								index_++;
							}

							to = index_;

							if( from != to )
							{	
								this->description.erase( from, (to - from + 1) );
								this->description.insert( from, sizeInfoHeight );
							}
						}
						else if( IsSingle == true )
						{
							while( strncmp( this->description.c_str() + index_, "width=", 6) != 0 )
							{
								index_++;
							}
							index_ = index_ + 5;
							
							from = index_;
							
							index_++;
							while( strncmp( this->description.c_str() + index_, "'", 1) != 0 )
							{
								index_++;
							}
							
							to = index_;
							
							if( from != to )
							{
								this->description.erase( from, to - from + 1 );
								this->description.insert( from, sizeInfoWidth );
							}
							from = -1;
							to = -1;
							while( strncmp( this->description.c_str() + index, "height=", 7 ) != 0 )
							{
								index_++;
							}
							index_ = index_ + 6;
							from = index_;
							index_++;
							while( strncmp( this->description.c_str() + index_, "'", 1) != 0 )
							{
								index_++;
							}
							to = index_;
							if( from != to )
							{
								this->description.erase( from, to - from + 1 );
								this->description.insert( from, sizeInfoHeight );
							}

						}
						else if( IsNon == true )
						{
							while( strncmp( this->description.c_str() + index_, "width=", 6 ) != 0 )
							{
								index_++;
							}
							index_++;
							while( strncmp( this->description.c_str() + index_, "=", 1 ) != 0 )
							{
								index_++;
							}
							index_++;
							from = index_;

							while( strncmp( this->description.c_str() + index_, " ", 1 ) != 0 )
							{
								index_++;
							}
							index_ = index_ - 1;
							to = index_;

							if( from != to )
							{
								this->description.erase( from, (to - from + 1 ) );
								char sizeOfwidth[3] = "";
								sprintf( sizeOfwidth, "%d", this->imageWidth );
								this->description.insert( from, sizeOfwidth );
							}

							from = -1;
							to = -1;

							while( strncmp( this->description.c_str() + index_, "height=", 7 ) != 0 )
							{
								index_++;
							}
							index_++;
							while( strncmp( this->description.c_str() + index_, "=", 1 ) != 0 )
							{
								index_++;
							}
							index_++;
							from = index_;

							while( strncmp( this->description.c_str() + index_, " ", 1) != 0 ||
									strncmp( this->description.c_str() + index_, ">", 1 ) != 0 )
							{
								index_++;
							}
							
							to = index_ - 1;

							if( from != to )
							{
								this->description.erase( from, to - from + 1 );
								char sizeOfheight[3] = "";
								sprintf( sizeOfheight, "%d", this->imageHeight );
								this->description.insert( from, sizeOfheight );
							}

						}

					}
				}//endif
				else if( flag_width == true && flag_height == false )//width만 존재
				{
					//width의 정보를 바꾸고 그 뒤에 height정보를 삽입한다.
					int from = -1;
					int to = -1;
					
					index_ = IsDeclareWidth;
					
					if( IsDouble == true ) 
					{
						while( strncmp( this->description.c_str() + index_, "width=", 6) != 0 )
						{
							index_++;
						}
						
						index_ = index_ + 5;

						from = index_;

						index_++;

						while( strncmp( this->description.c_str() + index_, "\"", 1) != 0 )
						{
							index_++;
						}

						to = index_;
						if( from != to )
						{
							this->description.erase( from,  to - from + 1 );
							this->description.insert( from, sizeInfoWidth );
						}
						index_ = to + 1;

						this->description.insert( index_, "height=");

						while( strncmp( this->description.c_str() + index_ ,"=", 1) != 0 )
						{
							index_++;
						}
						index_++;
						this->description.insert( index_ , sizeInfoHeight );
						
					}
					else if( IsSingle == true )
					{
						while( strncmp( this->description.c_str() + index_, "width=", 6 ) != 0 )
						{
							index_++;
						}
						index_ = index_ + 5;
						from = index_;
						index_++;
						while( strncmp( this->description.c_str() + index_, "'", 1 ) != 0 )
						{
							index_++;
						}
						to = index_;
						if( from != to )
						{
							this->description.erase( from, to - from + 1 ); 
							this->description.insert( from, sizeInfoWidth );
						}
						index_ = to + 1;
						this->description.insert( index_, "height=");
						while( strncmp( this->description.c_str() + index_, "=", 1) != 0 )
						{
							index_++;
						}
						this->description.insert( index_, sizeInfoHeight );
					}
					else if( IsNon == true ) 
					{
						while( strncmp( this->description.c_str() + index_ , "width=", 6 ) != 0 )
						{
							index_++;
						}
						index_++;
						while( strncmp( this->description.c_str() + index_, "=", 1) != 0 )
						{
							index_++;
						}
						index_++;
						from = index_;

						while( strncmp( this->description.c_str() + index_, " " , 1 ) != 0 )
						{
							index_++;
						}
						to = index - 1;
						if( from != to )
						{
							this->description.erase(from, to - from + 1 );
							char sizeOfwidth[3] = "";
							sprintf( sizeOfwidth, "%d", this->imageWidth );
							this->description.insert( from, sizeOfwidth );

							index_ = to + 1;
							this->description.insert( index_, "height=" );

							index_++;
						
							while( strncmp( this->description.c_str() + index_, "=", 1 ) != 0 )
							{
								index_++;
							}
							index_++;
							char sizeOfheight[3] = "";
							sprintf( sizeOfheight, "%d", this->imageHeight );
							this->description.insert( index_, sizeOfheight );
						}
					}	
				}//end else if
				else if( flag_height == true && flag_width == false )
				{
	 				//height의 정보를 바꾸고 그 뒤에 width정보를 삽입한다.
					int from = -1;
					int to = -1;

					index_ = IsDeclareHeight;
					if( IsDouble == true )
					{
						while( strncmp( this->description.c_str() + index_, "height=", 7 ) != 0 )
						{
							index_++;
						}
						index_ = index_ + 6;

						from = index_;

						index_++;

						while( strncmp( this->description.c_str() + index_, "\"", 1 ) != 0 )
						{
							index_++;
						}

						to = index_;
						if( from != to )
						{
							this->description.erase( from, (to - from + 1) );
							this->description.insert( from, sizeInfoHeight );
						

							index_ = to + 1;
							this->description.insert( index_, "width=" );
 
							index_++;
							while( strncmp( this->description.c_str() + index_, "=", 1) != 0 )
							{
								index_++;
							}
							index_++;
							this->description.insert( index_, sizeInfoWidth );
						}
					}
					else if( IsSingle = true )
					{	
						while( strncmp( this->description.c_str() + index_, "height=", 7 ) != 0 )
						{
							index_++;
						}
						index_ = index_ + 6;
						from = index_;
						index_++;
						while( strncmp( this->description.c_str() + index_, "'", 1 ) != 0 )
						{
							index_++;
						}
						to = index_;
						if( from != to )
						{
							this->description.erase( from, to - from + 1);
							this->description.insert( from, sizeInfoHeight );
						}
						
						index_ = to + 1;
						this->description.insert( index_, "width=" );
						
						index_++;
						
						while( strncmp( this->description.c_str() + index_, "=", 1) != 0 )
						{
							index_++;
						}
						index_++;
						this->description.insert( index_, sizeInfoWidth );

					}
					else if( IsNon == true )
					{
						while( strncmp( this->description.c_str() + index_, "height=", 7) != 0 )
						{
							index_++;
						}
						index_++;
						
						while( strncmp( this->description.c_str() + index_, "=", 1 ) != 0 )
						{
							index_++;
						}
						index_++;
						from = index_;

						while( strncmp( this->description.c_str() + index_, " ", 1) != 0 )//|| 
						{
							index_++;
						}
						to = index_ - 1;

						if( from != to )
						{
							this->description.erase( from, to - from + 1 );
							char sizeOfheight[3] = "";
							sprintf( sizeOfheight, "%d", this->imageHeight );
							this->description.insert( from, sizeOfheight );
						
							index_ = to + 1;

							this->description.insert( index_, "width=" );
							index_++;
							
							while( strncmp( this->description.c_str() + index_, "=", 1 ) != 0 )
							{
								index_++;
							}
							index_++;
							char sizeOfwidth[3] ="";
							sprintf( sizeOfwidth, "%d", this->imageWidth );
							this->description.insert( index_, sizeOfwidth );
						}

					}

				}//end else if
				else
				{
					//이미지 주소 앞에 크기선언한다.
					int insertPo = index_;
					while( strncmp( this->description.c_str() + insertPo, "src",3) != 0 )
					{
						insertPo++;
					}
					
					this->description.insert( insertPo - 1 , "width=220 height=190 ");
				}	
				*/	
				}
			}

			//cout<<" 임시 파일출력이닷!!! : "<<temp_description<<endl;
			//찾은 부분 뒤부터 다음 이미지를 찾는다.
			temp_description.assign( temp_description, index , temp_description.length() - index );
	
			index = -1;

			index = (int)( temp_description.find( "<img" ) );//tistory case
			
			if( index < 0 )
			{
				index = (int)( temp_description.find( "<IMG") );

				if( index > 0 )
				{

					start_IMGtag = index;
					
					index++;
					
					while( strncmp( temp_description.c_str() + index , "src", 3 ) != 0 )
					{
						index++;
					}

					index = index + 4;
					
					if( strncmp( temp_description.c_str() + index, "\"", 1 ) == 0 )
					{
						IsDouble = true;
					}
					else if( strncmp( temp_description.c_str() + index, "'", 1) == 0 )
					{
						IsSingle = true;
					}
					else
					{
						IsNon = true;
					}
				}
			}
			else
			{
				start_IMGtag = index;
				
				index++;
				
				while( strncmp(temp_description.c_str() + index, "src", 3 ) != 0 )
				{
					index++;
				}

				index = index + 4;
				
				if( strncmp( temp_description.c_str() + index, "\"", 1 ) == 0 )
				{
					IsDouble = true;
				}
				else if( strncmp( temp_description.c_str() + index, "'", 1) == 0 )
				{
					IsSingle = true;
				}
				else
				{
					IsNon = true;
				}
			}
			//	start_IMGtag = index;

			cout<<"hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"<<endl;
			//cout<<" tempdesc @@@@@ : "<<temp_description <<endl;
				
		}//end if
		//cout<<" this->description : "<<this->description<<endl;
		
	}//end while
	cout<<" this->description : "<<this->description<<endl;
	cout<<"이미지 변환 끝!!!!!!!!11"<<endl;
	
}

void Optimizer::FilteringEmbedTag()
{

//	string copyCommand("cp play_mediafile.jpg sendContents/images/");
//	system( copyCommand.c_str() );
	
	int start = -1;
	int end = -1;
	int index = -1;
	int i= -1;

	start = (int)( this->description.find("<embed") );
	
	while( start > 0 )
	{
		string copyCommand("cp play_mediafile.jpg sendContents/images/");
		system( copyCommand.c_str() );

		i++;
		index = start;
		int srcStart = -1;
		int srcEnd = -1;
		
		while( strncmp( this->description.c_str() + index, "src", 1 ) != 0 )
		{
			index++;
		}
		index++;
		while( strncmp( this->description.c_str() + index, "=", 1) != 0 )
		{
			index++;
		}
		index += 2;
		srcStart = index;
		index++;
		while( strncmp( this->description.c_str() + index, " ", 1 ) != 0 )
		{
			index++;
		}
		srcEnd = index;

		string srcTagStorage("");

		//src주소 저장
		srcTagStorage.assign( this->description, srcStart, srcEnd - srcStart + 1 );
			
		index++;
		while( strncmp( this->description.c_str() + index, ">", 1 ) != 0 )
		{
			index++;
		}
		end = index;

		//embed 태그를 모두 삭제하고 그 자리에 새로 만들어진 링크태그를 삽입한다.
		this->description.erase( start, end - start + 1);
		
		string insertTotalTag( "" );
		string insertLinkTag( "<a href=\"4000" );
		//string insertImgTag( "<img src=\"/mnt/mtd/nfsdisk/Url/images/play_mediafile.jpg\"></a>" );
		string insertImgTag( "<img src=\"/mnt/mtd/nfsdisk/Url/images/play_mediafile.jpg\"></a>" );
		insertTotalTag = insertLinkTag + srcTagStorage + ">" + insertImgTag;

		this->description.insert( start, insertTotalTag.c_str() );

		start = -1;
		start = (int)( this->description.find( "<embed" ) );
	}

}

void Optimizer::MakeHtmlDocument()
{
	
	string startHtml("<html>");
	string endHtml("</html>");
	string bgColor("<body bgcolor=\"white\">");
	//string fontColor("<font color=\"gold\">");
	//string fontEnd("</font>");
	string encoding("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
	
	string htmlDocument = "";
	
	htmlDocument = startHtml + bgColor + encoding + this->description + endHtml;

	this->html = htmlDocument;

	FILE* file = fopen( "newHtml.html", "wt");

	if( file != NULL )
	{
		fprintf( file, "%s\n", this->html.c_str() );
	}
	fclose(file);
	
}


bool Optimizer::MakeSendingZipFile()
{
	int flag = -1;
	bool reValue = false;

	string moveCommand("mv newHtml.html sendContents/html/");

	flag = system( moveCommand.c_str() );

	if(flag == 0 )
	{
		flag = -1;
		
		//1.전송할 폴더를 압축한다.
		string zipCommand("tar cvzf item.tar.gz sendContents");
	
		flag = system( zipCommand.c_str() );

		if( flag == 0 )
		{
			//2.본 폴더를 삭제한다.
			string removeDir("rm -r sendContents");

			flag = -1;

			flag = system( removeDir.c_str() );

			if( flag != 0 )
			{
				cout<<" error remove Directory! "<<endl;
			}
			else
			{
				reValue = true;
			}
		
		}
		else
		{
			cout<<" error zip Forder!"<<endl;
		}
	}
	else
	{
		cout<<" error move html! "<<endl;
	}

	return reValue;

}

void Optimizer::RemoveSendedZipFile()
{
	int flag = -1;
	
	string remove("rm -r item.tar.gz");

	flag = system( remove.c_str() );

	if( flag != 0 )
	{
		cout<<" error remove zipfile! "<<endl;
	}
	
}

#if 0
int main()
{
	
	Optimizer op;
// --- daum blog ---
//	op.SetSearchKey( "http://blog.daum.net/m1691/10991887");
	//http://blog.daum.net/chocomania/2275578?_top_blogtop=themblog1"
	//http://blog.daum.net/grandbleu" );
	
// ---- naver.com ----
	op.SetSearchKey( "http://uccviewer.tistory.com/147" );
	//end = (int)( this->description.find_last_of( "style=\"CURSOR: hand\" onclick=popview(this.src)" ) );
//"http://blog.naver.com/tkdrud3851/60034328746"
//			http://blog.naver.com/ladyfirst112/120034538210
//  --- egloos test blog ---
//    op.SetSearchKey( "http://daeil.tistory.com/108" );
//    op.SetSearchKey( "http://glen.egloos.com/2972845" );
//			http://ewriter.egloos.com/1501081"
			
//			http://yuius.egloos.com/2928811

//  --- tistory test blog ----
//	op.SetSearchKey( "http://uccviewer.tistory.com/138" );
//	op.SetSearchKey( "http://talkingof.tistory.com/14" );
//	op.SetSearchKey( "http://phoenixiel.tistory.com/45" );
//	op.SetSearchKey( "http://wansofamily.tistory.com/12" );
//	cout<<" key : "<<op.GetDescKey()<<endl;



//  --- egloos test title ---

	op.ReadDescription( "동영상" );
//	op.ReadDescription("blog.daum.net/xml/rss");
//	op.ReadDescription( "이동국의 EPL에서의 활약을 기대함" );

//	cout<<" Description : "<<op.GetDescription()<<endl;

//  --- tistory test title ---
//	op.ReadDescription("뇌 나이 측정");
//	op.ReadDescription("심심해");
//    cout<<" desc : "<<op.GetDescription()<<endl;

	op.RemoveUnessentialTag();
//	cout<<" desc : "<<op.GetDescription()<<endl;

	op.RenderingImage();
	
	op.FilteringEmbedTag();
		
	op.MakeHtmlDocument();
//	cout<<"html : "<<op.GetHtml()<<endl;

	cout<<op.GetDescription()<<endl;
	bool flag = false;
	flag = op.MakeSendingZipFile();

	if( flag == true )
	{
		//op.RemoveSendedZipFile();
	}
	
	return 0;
}
#endif
